package com.ruoyi.web.controller.exam;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.TExamPaperAnswer;
import com.ruoyi.exam.service.ITExamPaperAnswerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生试卷答案Controller
 *
 * @author Bandit
 * @date 2022-11-01
 */
@RestController
@RequestMapping("/exam/answer")
public class TExamPaperAnswerController extends BaseController
{
    @Autowired
    private ITExamPaperAnswerService tExamPaperAnswerService;

    /**
     * 查询学生试卷答案列表
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TExamPaperAnswer tExamPaperAnswer)
    {
        startPage();
        List<TExamPaperAnswer> list = tExamPaperAnswerService.selectTExamPaperAnswerList(tExamPaperAnswer);
        return getDataTable(list);
    }

    @GetMapping("/user")
    public AjaxResult getCurrentUserExam() {
        Long userId = SecurityUtils.getUserId();
        List<TExamPaperAnswer> list = tExamPaperAnswerService.selectTExamPaperAnswerByUserId(userId);
        return AjaxResult.success(list);
    }

    /**
     * 查询所有学生中未发卷的
     * @return
     */
    @GetMapping("/unDispatchStuList")
    public AjaxResult unDispatchStuList(Long examPaperId) {
        List<SysUser> list = tExamPaperAnswerService.unDispatchStuList(examPaperId);
        return AjaxResult.success(list);
    }

    /**
     * 导出学生试卷答案列表
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:export')")
    @Log(title = "学生试卷答案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TExamPaperAnswer tExamPaperAnswer)
    {
        List<TExamPaperAnswer> list = tExamPaperAnswerService.selectTExamPaperAnswerList(tExamPaperAnswer);
        ExcelUtil<TExamPaperAnswer> util = new ExcelUtil<TExamPaperAnswer>(TExamPaperAnswer.class);
        util.exportExcel(response, list, "学生试卷答案数据");
    }

    /**
     * 获取学生试卷答案详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tExamPaperAnswerService.selectTExamPaperAnswerById(id));
    }

    /**
     * 新增学生试卷答案
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:add')")
    @Log(title = "学生试卷答案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TExamPaperAnswer tExamPaperAnswer)
    {
        return toAjax(tExamPaperAnswerService.insertTExamPaperAnswer(tExamPaperAnswer));
    }

    @PreAuthorize("@ss.hasPermi('exam:answer:add')")
    @Log(title = "学生试卷答案", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<TExamPaperAnswer> tExamPaperAnswerList)
    {
        return toAjax(tExamPaperAnswerService.insertTExamPaperAnswerBatch(tExamPaperAnswerList));
    }

    /**
     * 修改学生试卷答案
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:edit')")
    @Log(title = "学生试卷答案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TExamPaperAnswer tExamPaperAnswer)
    {
        return toAjax(tExamPaperAnswerService.updateTExamPaperAnswer(tExamPaperAnswer));
    }

    /**
     * 删除学生试卷答案
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:remove')")
    @Log(title = "学生试卷答案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tExamPaperAnswerService.deleteTExamPaperAnswerByIds(ids));
    }
}
